package com.project.smarthome.login.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.view.inputmethod.InputMethodManager;

import com.project.smarthome.R;
import com.project.smarthome.base.BaseActivity;
import com.project.smarthome.base.NoUserAuthorize;
import com.project.smarthome.common.util.NetworkUtil;
import com.project.smarthome.common.util.StringUtils;
import com.project.smarthome.common.view.StatusBarUtil;
import com.project.smarthome.common.widget.AutoButton;
import com.project.smarthome.common.widget.AutoTextView;
import com.project.smarthome.common.widget.ImgEditText;
import com.project.smarthome.common.widget.SosDialog;
import com.project.smarthome.login.LoginContract;
import com.project.smarthome.login.adapter.UserHisAdapter;
import com.project.smarthome.login.model.NameAndPhone;
import com.project.smarthome.login.presenter.LoginPresenter;
import com.project.smarthome.main.view.MainActivity;
import com.sds.smarthome.foundation.util.XLog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by jie on 2018/3/21.
 */
public class LoginActivity extends BaseActivity implements LoginContract.View, TextWatcher, NoUserAuthorize {

    @BindView(R.id.edit_username)
    ImgEditText editUsername;
    @BindView(R.id.edit_pwd)
    ImgEditText editPwd;
    @BindView(R.id.btn_login)
    AutoButton btnLogin;
    @BindView(R.id.btn_regist)
    AutoButton btnRegist;
    @BindView(R.id.txt_forget)
    AutoTextView txtForget;
    @BindView(R.id.recycle_view)
    RecyclerView mRecycleView;

    private Unbinder mUnbinder;
    private LoginContract.Presenter mLoginPresenter;
    private InputMethodManager mInputMethodManager;

    @Override
    protected void setStatusBar() {
        StatusBarUtil.setTransparent(this);
    }

    @Override
    protected void initVariable() {
        mLoginPresenter = new LoginPresenter(this);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_login);
        mUnbinder = ButterKnife.bind(this);
        editUsername.setDrawableClick(new ImgEditText.IMyRightDrawableClick() {
            @Override
            public void rightDrawableClick() {
                editUsername.setText("");
                editUsername.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                editUsername.postInvalidate();
            }
        });

        editPwd.setDrawableClick(new ImgEditText.IMyRightDrawableClick() {
            @Override
            public void rightDrawableClick() {
                editPwd.setText("");
            }
        });
    }

    @Override
    protected void loadData() {
        mLoginPresenter.init();
    }

    @OnClick(R.id.btn_login)
    void login() {
        String userName = editUsername.getText().toString().trim();
        String passWord = editPwd.getText().toString().trim();
        if (!NetworkUtil.hasNetwork(this)) {
            showLoginError("不能连接服务器");
            return;
        }
        if (StringUtils.isEmpty(userName) || !StringUtils.isTel(userName)) {
            showLoginError(getResources().getString(R.string.login_phoneVerifyHint));
            return;
        }
        if (TextUtils.isEmpty(passWord)) {
            showLoginError("请输入密码");
            return;
        }
        if (passWord.length() < 6 || passWord.length() > 16) {
            showLoginError("密码长度不能小于6位或大于16位");
            return;
        }
        mLoginPresenter.toLogin(userName, passWord);

    }

    @OnClick(R.id.btn_regist)
    void register() {
        startActivity(this, RegistActivity.class);
    }

    @OnClick(R.id.txt_forget)
    void forgot() {
//        startActivity(this, PassWordActivity.class);
    }


    /**************************************************
     * 异步方法回调区
     ***************************************************/
    @Override
    public void showLoginError(String errmsg) {
        SosDialog dialog = new SosDialog(this);
        dialog.getDialog(this, errmsg, "知道了");
    }

    @Override
    public void loginSuccessToMain() {
        mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMethodManager.hideSoftInputFromWindow(LoginActivity.this.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
        startActivity(this, MainActivity.class);
        XLog.e("time");
        finish();
    }

    @Override
    public void showUserName(String username) {
        editUsername.setText(username);
    }

    @Override
    public void showLoginHistory(List<NameAndPhone> accountList) {
        final UserHisAdapter adapter = new UserHisAdapter(this);
        adapter.setItemClickListener(new UserHisAdapter.ItemClickListener() {
            @Override
            public void onClickItem(int pos) {
                NameAndPhone user = adapter.getItem(pos);
                editUsername.setText(user.getPhone());
                editPwd.setText("");
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        layoutManager.setStackFromEnd(false);  //自动到最后一条
        mRecycleView.setLayoutManager(layoutManager);
        mRecycleView.setHasFixedSize(true);
        adapter.setList(accountList);
        mRecycleView.setAdapter(adapter);
    }

    /**************************************************
     * 异步方法回调区-end
     ***************************************************/

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        mLoginPresenter.destroy();
        mInputMethodManager = null;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        editPwd.setText("");
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
